package com.jobsys.common;

import java.util.List;

import com.jfinal.plugin.activerecord.Db;
import com.jobsys.model.MBaseParam;

public class Common {
	/**
	 * 获取参数
	 * 
	 * @param name
	 * @return
	 */
	public static List<MBaseParam> getParamsByCache() {
		List<MBaseParam> params = MBaseParam.dao.findByCache("baseParamCache", "baseParamCache_key",
				"select * from baseparam where isDelete=0");

		return params;
	}

	/**
	 * 获取参数值
	 * 
	 * @param name
	 * @return
	 */
	public static MBaseParam getParamByNameFromCache(String name) {
		List<MBaseParam> params = getParamsByCache();

		MBaseParam param = null;
		for (int i = 0; i < params.size(); i++) {
			if (params.get(i).getStr("name").equals(name)) {
				param = params.get(i);
				break;
			}
		}

		return param;
	}

	/**
	 * 获取参数值
	 * 
	 * @param name
	 * @return
	 */
	public static MBaseParam getParamByCodeFromCache(String code) {
		List<MBaseParam> params = getParamsByCache();

		MBaseParam param = null;
		for (int i = 0; i < params.size(); i++) {
			if (params.get(i).getStr("code").equals(code)) {
				param = params.get(i);
				break;
			}
		}

		return param;
	}

	/**
	 * 获取参数
	 * 
	 * @param name
	 * @return
	 */
	public static List<MBaseParam> getParams(String name) {
		List<MBaseParam> params = MBaseParam.dao.find("select * from baseparam where isDelete=0 and name=?", name);

		return params;
	}

	/**
	 * 获取参数
	 * 
	 * @param name
	 * @return
	 */
	public static MBaseParam getParamByName(String name) {
		MBaseParam params = MBaseParam.dao.findFirst("select * from baseparam where isDelete=0 and name=?", name);

		return params;
	}

	/**
	 * 获取参数
	 * 
	 * @param name
	 * @return
	 */
	public static MBaseParam getParamByCode(String code) {
		MBaseParam params = MBaseParam.dao.findFirst("select * from baseparam where isDelete=0 and code=?", code);

		return params;
	}

	/**
	 * 更新基础参数
	 * 
	 * @param name
	 * @param value
	 * @return
	 */
	public static boolean updateParamValue(String name, String value) {
		int result = Db.update("update marketdb.baseparam set value=? where name=? and isDelete=0", value, name);
		if (result > 0) {
			return true;
		} else {
			return false;
		}
	}
}
